//
//  AKOTImageProvider.m
//  AKMac
//
//  Created by Ben Chen on 6/3/08.
//  Copyright 2008 Sleeper. All rights reserved.
//

#import "AKOTImageProvider.h"

@implementation AKOTImageProvider

- (CGImageRef)preview {
	return [self backgroundImage];
}

// Return a autoreleased CGImageRef
- (CGImageRef)imageRefWithName:(NSString*)fileName type:(NSString*)type {
	CGImageRef retVal;
	
#ifndef AKIPHONE
	CGImageSourceRef source;
	source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:
												   [[NSBundle mainBundle] pathForResource:fileName ofType:type]], NULL);
	retVal = CGImageSourceCreateImageAtIndex(source, 0, NULL);
	CFRelease(source);
	[(id)retVal autorelease];
#else
	UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.%@", fileName, type]];
	retVal = [image CGImage];
#endif
	
	return retVal;
}	

- (CGImageRef)imageForPiece:(AKPiece)piece {
	CGImageRef retVal = NULL;
	
	if (piece.side == AKOTBlack)
		retVal = [self imageRefWithName:@"AKOTBlack" type:@"png"];
	else
		retVal = [self imageRefWithName:@"AKOTWhite" type:@"png"];
	
	return retVal;
}

- (CGImageRef)backgroundImage {
	return [self imageRefWithName:@"AKOTBoard" type:@"png"];
}

@end
